//
//  TVAdditionalCellModel.swift
//  StarTimesON
//
//  Created by Toj on 12/8/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class TVAdditionalCellModel: UICollectionViewCell {
    
    var cellModel: TVAdditionalCommodityCellModel? { didSet {
        guard let model = cellModel else { return }
        
        // 时间
        nameView.setTitle(model.name, for: .normal)
        nameView.isSelected = model.isSelected
        productLabel.text = model.productName
        
        let imgName = model.isSelected ? "commodity_border_sel" : ""
        backImageView.image = imgName.to_image
        
        // 商品折扣价
        priceLabel.attributedText = model.priceString
        
        // 商品吊牌价
        listPriceLabel.attributedText = model.listPriceString
        listPriceLabel.isHidden = model.listPrice <= model.price
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    // 选择边框View
    private lazy var backImageView: UIImageView = UIImageView()
    
    // 名称 + 背景图
    private lazy var nameView: UIButton = {
        let view = UIButton()
        view.isUserInteractionEnabled = false
        view.titleLabel?.font = UIFont.systemFont(ofSize: 16.f_scale)
        view.setTitleColor("#99773C".to_color, for: .normal)
        view.setTitleColor("#775E33".to_color, for: .selected)
        view.setBackgroundImage("commodity_mb_date_bgimg_normal".to_image, for: .normal)
        view.setBackgroundImage("commodity_mb_date_bgimg_sel".to_image, for: .selected)
        view.st_shadow(color: .black, opacity: 0.1, shadowOffset: CGSize(width: 4, height: 2))
        return view
    }()
    
    // 现价格
    private lazy var priceLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#554422".to_color
        return view
    }()
    
    // 原价
    private lazy var listPriceLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#AAAAAA".to_color
        view.font = UIFont.regular(with: 14.f_scale)
        view.textAlignment = .center
        return view
    }()
    
    // 商品名称
    private lazy var productView: UIView = {
        let view = UIView()
        view.backgroundColor = "#FFEEC0".to_color
        view.st_cornerRadius = discountLabelHeight * 0.5
        return view
    }()
    
    private lazy var productLabel: UILabel = {
        let view = UILabel()
        view.textAlignment = .center
        view.textColor = "#99773C".to_color
        view.font = UIFont.systemFont(ofSize: 10)
        return view
    }()
    
    private let discountLabelHeight: CGFloat = 12
}

// MARK: - 初始化
extension TVAdditionalCellModel {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(nameView)
        contentView.addSubview(backImageView)
        contentView.addSubview(priceLabel)
        contentView.addSubview(listPriceLabel)
        contentView.addSubview(productView)
        productView.addSubview(productLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        backImageView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        nameView.snp.makeConstraints { make in
            make.top.left.right.equalTo(contentView)
            make.height.equalTo(32)
        }
        
        priceLabel.snp.makeConstraints { make in
            make.top.equalTo(nameView.snp.bottom).offset(4)
            make.left.right.equalTo(contentView)
            make.height.equalTo(24)
        }
        
        listPriceLabel.snp.makeConstraints { make in
            make.top.equalTo(priceLabel.snp.bottom).offset(4)
            make.left.right.equalTo(contentView)
            make.height.equalTo(14)
        }
        
        productView.snp.makeConstraints { make in
            make.bottom.equalTo(contentView.snp.bottom).offset(-12)
            make.left.right.equalTo(contentView).inset(8)
            make.height.equalTo(discountLabelHeight)
        }
        
        productLabel.snp.makeConstraints { make in
            make.left.right.equalTo(productView).inset(4)
            make.top.bottom.equalTo(productView)
        }
    }
}
